package com.efounder.frame.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.efounder.mobilemanager.R;

import java.io.Serializable;
import java.util.ArrayList;


public class EFPopupMenu {
	private ArrayList<PopupMenuItemModel> itemList;
	private Context context;
	private PopupWindow popupWindow;
	private ListView listView;
	private PopAdapter adapter;
	/**  menu的根layout */
	private LinearLayout rootMenuLL;
	/**  菜单小项背景 */
	private int menuItemBackgroundResid = -1;
	/**  字体颜色 */
	private ColorStateList textColorStateList;
	private int textSize = -1;
	
	public EFPopupMenu(Context context) {
		this.context = context;

		itemList = new ArrayList<PopupMenuItemModel>(5);
		
		View view = LayoutInflater.from(context).inflate(R.layout.ef_popmenu, null);
        
        initView(view);
	}
	
	@SuppressWarnings("deprecation")
	private void initView(View view){
        //设置 listView
        listView = (ListView)view.findViewById(R.id.listView);
        adapter = new PopAdapter();
        listView.setAdapter(adapter);
        popupWindow = new PopupWindow(view, (int) context.getResources().getDimension(R.dimen.ef_pop_menu_width), LayoutParams.WRAP_CONTENT,true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景（很神奇的）
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
		rootMenuLL = (LinearLayout) view.findViewById(R.id.popup_view_cont);
	}
	/**
	 * 设置菜单项点击监听器
	 * @param listener
	 */
	public void setOnItemClickListener(OnItemClickListener listener) {
		listView.setOnItemClickListener(listener);
	}

	/**
	 * 批量添加菜单项
	 * @param itemList
	 */
	public void addItems(ArrayList<PopupMenuItemModel> itemList) {
		for (PopupMenuItemModel itemModel : itemList)
			this.itemList.add(itemModel);
	}

	/**
	 * 单个添加菜单项
	 * @param item
	 */
	public void addItem(PopupMenuItemModel item) {
		itemList.add(item);
	}

	/**
	 * 下拉式 弹出 pop菜单
	 * @param anchor
	 */
	public void show(View anchor) {
		//计算X轴偏移
		int xOff =  - popupWindow.getWidth() + anchor.getWidth() - (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,5,anchor.getResources().getDisplayMetrics());
		popupWindow.showAsDropDown(anchor, xOff, 0);
		// 使其聚集
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        //刷新状态
        popupWindow.update();
	}
	
	/**
	 * 隐藏菜单
	 */
	public void dismiss() {
		popupWindow.dismiss();
	}

	// 适配器
	private final class PopAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return itemList.size();
		}

		@Override
		public PopupMenuItemModel getItem(int position) {
			return itemList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.ef_pomenu_item, null);
				holder = new ViewHolder();

				convertView.setTag(holder);

				holder.groupItem = (TextView) convertView.findViewById(R.id.textView);
				holder.groupItem.setBackgroundResource(R.drawable.ef_selector_transparent_background);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			PopupMenuItemModel itemModel = itemList.get(position);
			//设置文本
			holder.groupItem.setText(itemModel.getText());
			//设置图片
			Drawable drawable = Drawable.createFromPath(itemModel.getImagePath());
			if (drawable != null){
				drawable.setBounds(0,0, (int) (holder.groupItem.getTextSize() * 3 / 2),(int) (holder.groupItem.getTextSize() * 3 / 2));
				holder.groupItem.setCompoundDrawables(drawable,null,null,null);
			}
			if (menuItemBackgroundResid != -1)
				holder.groupItem.setBackgroundResource(menuItemBackgroundResid);
			if (textColorStateList != null) 
				holder.groupItem.setTextColor(textColorStateList);
			if (textSize != -1) {
				holder.groupItem.setTextSize(textSize);
			}
			return convertView;
		}

		private final class ViewHolder {
			TextView groupItem;
		}
	}

	public class PopupMenuItemModel implements Serializable {
		private String imagePath;
		private String text;

		public String getImagePath() {
			return imagePath;
		}

		public void setImagePath(String imagePath) {
			this.imagePath = imagePath;
		}

		public String getText() {
			return text;
		}

		public void setText(String text) {
			this.text = text;
		}
	}


	/**
	 * 设置PopMenu背景色
	 * @param resid
	 */
	public void setBackgroundResource(int resid){
		rootMenuLL.setBackgroundResource(resid);
	}
	
	/**
	 * 设置PopMenu背景色
	 * @param color
	 */
	public void setBackgroundColor(int color){
		rootMenuLL.setBackgroundColor(color);
	}
	
	/**
	 * 设置菜单小项背景
	 * @param resid 最好传入一个selector
	 */
	public void setMenuItemBackground(int resid){
		this.menuItemBackgroundResid = resid;
//		adapter.notifyDataSetChanged();
	}
	
	/**
	 * 设置字体颜色
	 * @param resid 
	 */
	public void setTextColorResid(int resid){
		this.textColorStateList = context.getResources().getColorStateList(resid);
	}
	
	/**
	 * 设置字体大小
	 * @param textSize
	 */
	public void setTextSize(int textSize){
		this.textSize = textSize;
	}

	/**
	 * 是否正在显示
	 * @return
     */
	public boolean isShowing() {
		return popupWindow.isShowing();
	}
}
